import common.defines as defines
import cardhand
import wx
import time

class Player(object):

    _bust = False
    _stand = False
    _blackjack = False
    _21 = False
    hands = []
    _current_hand = None
    _current_bet = 0
    myTurn = False

    def __init__(self, name, bank=0):
        self.name = name
        self.myTurn = False
        self.playingCurrentHand = False
        self._bank = bank
    def _get_bank(self):
        return self._bank
    def _set_bank(self, val):
        self._bank = val
    bank = property(_get_bank, _set_bank)

    def _get_current_bet(self):
        return self._current_bet
    def _set_current_bet(self, bet):
        self._current_bet = bet
        self._bank -= bet
    current_bet = property(_get_current_bet, _set_current_bet)

    def _get_stand(self):
        return self._stand
    is_standing = property(_get_stand)

    def stand(self):
        self._stand = True

    def _get_bust(self):
        return self._bust
    is_bust = property(_get_bust)

    def new_hand(self, card1, card2):
        self.hands = []
        self.hands.append(cardhand.Hand(card1, card2))
        self._current_hand = self.hands[0]
        if self._current_hand.total == 21:
            self._blackjack = True

    def _get_current_hand(self):
        return self._current_hand.cards
    current_hand = property(_get_current_hand)

    def _get_total(self):
        return self._current_hand.total
    total = property(_get_total)

    def display_total(self):
        print self._current_hand.total

    def hit(self, card):
        card.display()
        self._current_hand.deal_card(card)
        if self._current_hand.total == 21:
            self._21 = True
        if self._current_hand.total > 21:
            self._bust = True
            print "BUST!"

    def display_hand(self):
        print "\n"
        print self.name
        self._current_hand.display_hand()

    def render_hand(self, dc, sprite, chip, position, dealer=False):
#        print 'Rendering inside cardplayer'
        if not dealer:
            x1_base = position * 160 + 30
            y1_base = 270
        else:
            x1_base = 2 * 160 + 30
            y1_base = defines.CARD_WIDTH / 2 + 5
        if self._current_hand != None:
#            print self.name + "'s hand is not empty"
            for i, card in enumerate(self.current_hand):
                if card:
                    x = card.face * defines.X_INDEX_MULT + 1
                    y = card.suit * defines.Y_INDEX_MULT + 1
                else:
                    x = 0
                    y = 4 * defines.Y_INDEX_MULT + 1
                x1 = (i * 30) + x1_base
                if not dealer:
                    y1 = -(i * 35) + y1_base
                else:
                    y1 = -35 + y1_base
                dc.DrawBitmap(sprite.GetSubBitmap(wx.Rect(x, y, defines.CARD_WIDTH, defines.CARD_HEIGHT)), x1, y1)

            font = wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTWEIGHT_NORMAL, wx.FONTSTYLE_NORMAL)
            dc.SetFont(font)
            if not dealer:
                dc.DrawText(self.name + ": " + str(self.total), x1_base , y1_base + defines.CARD_HEIGHT + 5)
            else:
                dc.DrawRotatedText("Dealer: " + str(self.total), x1_base - 30, y1_base + defines.CARD_HEIGHT / 2 + 15, 90)
            if self.myTurn == True:
                dc.DrawBitmap(chip, x1 + 35, y1)

    def status(self):
        self.display_hand()
        self.display_total()

    def reset(self):
        self._bust = False
        self._stand = False
#        self._current_bet = False
        self._blackjack = False
        self._21 = cardhand.Hand()

    def __str__(self):
        hand = self._get_current_hand()
        returnMe = '{'
        for card in hand:
            returnMe += '[' + str(card) + ']'
        returnMe += '}'
        return returnMe

    def __repr__(self):
        hand = self._get_current_hand()
        return repr(hand[0]) + repr(hand[1])
